package net.hvacapp.cmvrfforandroid.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import hirondelle.date4j.DateTime;

public class DateTimeUtils {

    /**
     * 获取系统时间
     *
     * @return
     */
    public static long getSystemTime() {
        return System.currentTimeMillis();
    }

    public static int getIntegerTime() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    /**
     * 获取短时间格式
     *
     * @param time
     * @return
     */
    public static String getShort(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYY-MM-DD");
    }

    public static String getShortTime(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYY/MM/DD");
    }

    public static String getDate (String time) {
        // 准备第一个模板，从字符串中提取出日期数字
        String pat1 = "yyyyMMddHHmmss" ;
        // 准备第二个模板，将提取后的日期数字变为指定的格式
        String pat2 = "yyyy年MM月dd日" ;
        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;        // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;        // 实例化模板对象
        Date d = null ;
        try{
            d = sdf1.parse(time) ;   // 将给定的字符串中的日期提取出来
        }catch(Exception e){            // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace() ;       // 打印异常信息
        }
        return sdf2.format(d);
    }

    /**
     * 获取长时间格式
     *
     * @param time
     * @return String:YYYY-MM-DD HH:MM:SS
     */
    public static String getLong(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYY-MM-DD hh:mm:ss");
    }

    /**
     * 获取长时间格式
     *
     * @param time
     * @return String:YYYY/MM/DD HH:MM
     */
    public static String getLongTime(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYY/MM/DD   hh:mm");
    }

    /**
     * 获取服务记录时间
     *
     * @param time 时间戳
     * @return MM-DD hh:mm
     */
    public static String getInfoDate(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("MM-DD hh:mm");
    }

    /**
     * 获取服务记录时间
     *
     * @param time 时间戳
     * @return MM/DD
     */
    public static String getDateInfo(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("MM/DD");
    }

    /**
     * 获取服务记录的时间
     *
     * @return 今天/昨天/本周/本月/更久
     */
    public static String getRecordsDate(long time) {
        // 需要比较的时间
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        // 今天
        DateTime todayTime = DateTime.today(TimeZone.getDefault());
        // 昨天
        DateTime yesTime = todayTime.minusDays(1);
        // 本周
        /*DateTime weekTime = null;
		int weekIndex = todayTime.getWeekDay();
		if (weekIndex > 1) {
			weekTime = todayTime.minusDays(weekIndex - 2);
		} else {
			weekTime = todayTime.minusDays(weekIndex - 2 + 7);
		}
		// 本月
		DateTime monthTime = todayTime.getStartOfMonth();*/

        if (dateTime.gteq(todayTime)) {
            return "今天";
        } else if (dateTime.gteq(yesTime)) {
            return "昨天";
        } else {
            return getDateInfo(time);
        }
    }

    /**
     * 根据日期取得星期几
     *
     * @param time 时间戳
     * @return 星期几 dateTime.getWeekDay() [1-7][星期日-星期六]
     */
    public static String getWeek(long time) {
        String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return weeks[dateTime.getWeekDay() - 1];
    }

    /**
     * 根据日期获取当前日期处于几号
     *
     * @param time
     * @return
     */
    public static String getDayNum(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.getDay() + "号";
    }

    /**
     * 根据日期获取当前日期处于几号
     *
     * @param time
     * @return
     */
    public static String getDayNumNo(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.getDay()+"";
    }

    /**
     * 获得time1和time2相差的天数 （只考虑最大相差1个月的天数）
     *
     * @param time1
     * @param time2
     * @return 天数
     */
    public static int getDayNum(long time1, long time2) {
        DateTime dateTime1 = DateTime.forInstant(time1, TimeZone.getDefault());
        DateTime dateTime2 = DateTime.forInstant(time2, TimeZone.getDefault());
        int day1 = dateTime1.getDay();
        int day2 = dateTime2.getDay();
        int day = 0;

        if (dateTime1.getMonth() != dateTime2.getMonth()) {
			/*不在同一个月*/
            day = day1 + dateTime2.getNumDaysInMonth() - day2;
        } else {
			/*在同一个月*/
            day = day1 - day2;
        }

        return day;
    }

    /**
     * 获取xx年xx月xx日 xx:xx:xx 格式
     *
     * @param time
     * @return
     */
    public static String getString(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYYMMDD hh:mm");
    }

    /**
     * 获取20141414格式
     *
     * @param time
     * @return
     */
    public static String getOnlyNumber(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("YYYYMMDD");
    }

    /**
     * 获取DateTime
     *
     * @param time
     * @return
     */
    public static DateTime getDateTime(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime;
    }

    /**
     * 获取hh:mm格式
     *
     * @param time
     * @return
     */
    public static String getTime(long time) {
        DateTime dateTime = DateTime.forInstant(time, TimeZone.getDefault());
        return dateTime.format("hh:mm");
    }

    /**
     * 获取YYYY-MM格式的时间
     *
     * @param dateTime
     * @return
     */
    public static String getYearAndMonth(DateTime dateTime) {
        return dateTime.format("YYYY-MM");
    }
}
